package jmine.tec.web.wicket.pages.example.execute;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import jmine.tec.report.impl.table.ReportTableBuilder;
import jmine.tec.web.wicket.component.command.button.ButtonCommand;
import jmine.tec.web.wicket.component.command.button.SingleEntityExecutionButton;
import jmine.tec.web.wicket.dao.WebTestEntityDAO;
import jmine.tec.web.wicket.domain.WebTestEntity;
import jmine.tec.web.wicket.mock.MockMessageCreator;
import jmine.tec.web.wicket.pages.form.ExecutePage;

import org.apache.wicket.markup.html.WebMarkupContainer;

import bancosys.tec.exception.MessageCreator;
import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.persist.dao.BaseDAOFactory;
import bancosys.tec.persist.dao.BeanNotFoundException;

/**
 * Exemplo de página que executa coisas.
 * 
 * @author Rafael Volpato
 * @date Nov 10, 2011
 */
public class ExecutePageExample extends ExecutePage<WebTestEntity, WebTestEntity> {

    /**
     * {@inheritDoc}
     */
    @Override
    protected WebTestEntity createModel() {
        BaseDAO<WebTestEntity> dao = this.daoFactoryRef.get().getGenericDAO(WebTestEntity.class);
        return dao.createBean();
    }

    /**
     * {@inheritDoc}
     */
    public List<WebTestEntity> search(BaseDAOFactory daoFactory) {
        WebTestEntityDAO dao = daoFactory.getDAOByClass(WebTestEntityDAO.class);
        WebTestEntity model = this.getModel();
        return dao.findByMnemonicoData(model.getMnemonico(), model.getData());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void addSearchFields(WebMarkupContainer container) {
        container.add(this.createTextField("mnemonico"));
        container.add(this.createDateField("data"));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void addResultTableColumns(ReportTableBuilder<WebTestEntity> table) {
        table.addStringColumn("nome", "Nome", "nome");
        table.addStringColumn("mnemonico", "Mnemônico", "mnemonico");
        table.addDateColumn("data", "Data", "data");
        table.addNumberColumn("valor", "Valor", "valor", 2);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected List<ButtonCommand> getPageCommands() {
        List<ButtonCommand> pageCommands = super.getPageCommands();

        pageCommands.add(new SingleEntityExecutionButton<WebTestEntity>() {

            /**
             * {@inheritDoc}
             */
            public String getLabel() {
                return "Executar";
            }

            /**
             * {@inheritDoc}
             */
            @Override
            protected void doExecute(WebTestEntity entity) {
                System.out.println("Executando comando da pagina para a entidade: " + entity);
            }

            /**
             * {@inheritDoc}
             */
            @Override
            protected Set<Serializable> getSelected() {
                return ExecutePageExample.this.getSelectedItens();
            }

            /**
             * {@inheritDoc}
             */
            @Override
            protected WebTestEntity loadEntity(Serializable entityId) throws BeanNotFoundException {
                return ExecutePageExample.this.loadEntity(entityId);
            }
        });

        return pageCommands;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected MessageCreator getHelpTextCreator() {
        return new MockMessageCreator();
    }
}